{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## _*Quantum SVM*_\n",
    "\n",
    "### Introduction\n",
    "\n",
    "Please refer to [this file](https://github.com/Qiskit/qiskit-tutorials/blob/master/legacy_tutorials/aqua/machine_learning/qsvm_classification.ipynb) for introduction."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from matplotlib import pyplot as plt\n",
    "import numpy as np\n",
    "from qiskit.ml.datasets import ad_hoc_data\n",
    "from qiskit import BasicAer\n",
    "from qiskit.aqua import QuantumInstance\n",
    "from qiskit.circuit.library import ZZFeatureMap\n",
    "from qiskit.aqua.algorithms import QSVM\n",
    "from qiskit.aqua.utils import split_dataset_to_data_and_labels, map_label_to_class_name"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First we prepare the dataset, which is used for training, testing and the finally prediction.\n",
    "\n",
    "*Note: You can easily switch to a different dataset, such as the Breast Cancer dataset, by replacing 'ad_hoc_data' to 'Breast_cancer' below.*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'A': 0, 'B': 1}\n"
     ]
    }
   ],
   "source": [
    "feature_dim = 2 # dimension of each data point\n",
    "training_dataset_size = 20\n",
    "testing_dataset_size = 10\n",
    "random_seed = 10598\n",
    "shots = 1024\n",
    "\n",
    "sample_Total, training_input, test_input, class_labels = ad_hoc_data(\n",
    "    training_size=training_dataset_size, \n",
    "    test_size=testing_dataset_size, \n",
    "    n=feature_dim, gap=0.3, plot_data=False\n",
    ")\n",
    "datapoints, class_to_label = split_dataset_to_data_and_labels(test_input)\n",
    "print(class_to_label)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we create the svm.\n",
    "- We build the svm instance by instantiating the class QSVM. \n",
    "- We build the feature map instance (required by the svm instance) by instantiating the class SecondOrderExpansion."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "backend = BasicAer.get_backend('qasm_simulator')\n",
    "feature_map = ZZFeatureMap(feature_dim, reps=2)\n",
    "svm = QSVM(feature_map, training_input, test_input, None)# the data for prediction can be fed later.\n",
    "svm.random_seed = random_seed\n",
    "quantum_instance = QuantumInstance(backend, shots=shots, seed_simulator=random_seed, seed_transpiler=random_seed)\n",
    "result = svm.run(quantum_instance)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let us check the result."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "kernel matrix during the training:\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD6CAYAAABnLjEDAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO2dd3hVZbbG3490CAESAoReBELoEOkgICUoVYERHYQRB3VExTJjuTMidkVlLKOCyBW90iygYIMBUUBpoYeaQICEKgmBQEggfPePnMyTc9a7JZNACOz1ex4eOC9rn7P3OXtl56y9vncZay0URbn2KXOld0BRlJJBk11RXIImu6K4BE12RXEJmuyK4hI02RXFJRQr2Y0xccaYncaYRGPME5dqpxRFufSYot5nN8b4AdgFoDeAFABrAYyw1m77nW3Ei5UtG0Zj/f0DhOZHNAA4l5MjtLKhZWlsRnqa0GrUq01jk3clCq1Fy+ZCS9ydTLe/cCFXaKFh/HgZZcP4MeRkyePNSDtBYyOiIoVWOaw8jT2XK/f31Nmzcr8CA+n2wUR3Or/KGEN1Rs7580I7T/YVAMoGBQntDDk/zmTJ4wKAkGC5vdO+Bvr7C82vDL9+ZpF9cHpvzp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C2jhxpjFxpjdnr95838pxhhTyxjzozFmmzEmwRjzkEe/qo/NGBNsjFljjNnkOa6JHr2eMWa155ycY4zhE0Svcko02T2TYP8FoB+AGAAjjDExJbkPl5iPAMT5aE8AWGKtbQhgiefx1cZ5AI9aa2MAdABwv+dzutqPLRtAT2ttSwCtAMQZYzoAeAXAZGvtdQDSAYy5gvt42SjpK3s7AInW2j3W2hwAswEMKuF9uGRYa38GkOYjDwKQv6RrBvJm119VWGsPWWvXe/59CsB2ADVwlR+bzSN/GV+A548F0BPA5x79qjuuwlLSyV4DQMG1iSke7VqiqrU2fwzLYQBVr+TOFBdjTF3kjexejWvg2IwxfsaYjQCOAlgMIAnACWtt/hD4a/GcBKAFusuKzbvVcdXe7jDGhAL4AsB4a+3Jgv93tR6btTbXWtsKQE3k/abJLW2vQUo62VMBFPSSrunRriWOGGOiAMDztxz4dhVgjAlAXqJ/aq390iNfE8cGANbaEwB+BNARQEVjTL6Ry7V4TgIo+WRfC6Chp/oZCOA2AF+X8D5cbr4GMMrz71EApPVJKcfkme5/CGC7tfaNAv91VR+bMSbSGFPR8+8QAL2RV4/4EUD+VMqr7rgKS4k31RhjbgLwTwB+AKZba18o0R24hBhjZgHojrxVU0cATAAwH8BcALWRt8JvuLXWt4hXqjHGdAGwHMAWAPkeVE8h73v7VXtsxpgWyCvA+SHvQjfXWvusMaY+8orF4QA2APijtZaPlL2K0Q46RXEJWqBTFJegya4oLkGTXVFcgia7orgETXZFcQma7IriEjTZFcUlaLIrikv4f1SPYn2fDAE5AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "testing success ratio:  1.0\n"
     ]
    }
   ],
   "source": [
    "print(\"kernel matrix during the training:\")\n",
    "kernel_matrix = result['kernel_matrix_training']\n",
    "img = plt.imshow(np.asmatrix(kernel_matrix),interpolation='nearest',origin='upper',cmap='bone_r')\n",
    "plt.show()\n",
    "\n",
    "print(\"testing success ratio: \", result['testing_accuracy'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "The programmatic approach allows the users to invoke APIs upon the svm instance directly. In the following, we invoke the API \"predict\" upon the trained svm instance to predict the labels for the newly provided data input.\n",
    "\n",
    "Use the trained model to evaluate data directly, and we store a `label_to_class` and `class_to_label` for helping converting between label and class name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ground truth: [0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1]\n",
      "preduction:   [0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1]\n"
     ]
    }
   ],
   "source": [
    "predicted_labels = svm.predict(datapoints[0])\n",
    "\n",
    "predicted_classes = map_label_to_class_name(predicted_labels, svm.label_to_class)\n",
    "print(\"ground truth: {}\".format(datapoints[1]))\n",
    "print(\"preduction:   {}\".format(predicted_labels))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
